/**
 * @filename:WorkSettingServiceImpl Feb 22, 2024
 * @project Wonder Vue Admin Api  1.1.0
 * Copyright(c) 2022 Wonder Co. Ltd.
 * All right reserved.
 */
package com.wonder.app.service.impl;

import cn.hutool.json.JSONObject;
import com.vtuzx.core.bean.dto.VtuzxMap;
import com.vtuzx.core.utils.VtuzxUtil;
import com.wonder.app.domain.*;
import com.wonder.app.mapper.WorkSettingDao;
import com.wonder.app.mapper.WorktestDao;
import com.wonder.app.mapper.WorkvsDao;
import com.wonder.app.service.WorkSettingService;
import com.wonder.app.request.WorkSettingRequest;
import com.wonder.app.service.WorktestService;
import com.wonder.app.service.WorkvsService;
import com.wonder.common.utils.StringUtils;
import com.wonder.common.utils.MessageUtils;
import com.wonder.common.common.AjaxResult;

import java.util.*;

import com.wonder.config.MqttProviderConfig;
import com.wonder.constant.BillType;
import com.wonder.constant.IXqcConst;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

/**
 * @Description:TODO(工单设置服务实现)
 *
 * @version: 1.1.0
 * @author: Wonder
 *
 */
@Service
public class WorkSettingServiceImpl  extends ServiceImpl<WorkSettingDao, WorkSetting> implements WorkSettingService  {


	@Autowired
	private WorkSettingService workSettingService;
	@Autowired
	private  WorkSettingDao workSettingDao;
	@Autowired
	private WorkvsService workvsService;
	@Autowired
	private WorkvsDao workvsDao;
	@Autowired
	private WorktestService worktestService;
	@Autowired
	private WorktestDao worktestDao;
	@Autowired
	private MqttProviderConfig providerConfig;

	/**
     * 验证工单设置信息
     *
     * @param workSetting
     * @param type
     * @return String
     */
    @Override
    public String validate(WorkSetting workSetting,String type){
    
    
       return "";
    }
    
    
    /**
	 * Description:工单设置分页返回
	 * @param request //检索条件
	 * @param page //分页参数
	 * @return IPage<WorkSetting>   //分页记录
	 */
	@Override
	public IPage<WorkSetting> selectList(WorkSettingRequest request, Page<WorkSetting> page){
	    LambdaQueryWrapper<WorkSetting> lambdaQueryWrapper = new LambdaQueryWrapper<>();
	    
	    if (null != request.getId()) {
	       lambdaQueryWrapper.eq(WorkSetting::getId, request.getId());
	      }               
             
        if (StringUtils.hasText(request.getFnumber())) {
			    lambdaQueryWrapper.like(WorkSetting::getFnumber, request.getFnumber()); 
		    } 
             
        if (StringUtils.hasText(request.getFbillno())) {
			    lambdaQueryWrapper.like(WorkSetting::getFbillno, request.getFbillno()); 
		    } 
             
        if (StringUtils.hasText(request.getFname())) {
			    lambdaQueryWrapper.like(WorkSetting::getFname, request.getFname()); 
		    } 
             
        if (StringUtils.hasText(request.getFcustomer())) {
			    lambdaQueryWrapper.like(WorkSetting::getFcustomer, request.getFcustomer()); 
		    } 
             
        if (StringUtils.hasText(request.getFheader())) {
			    lambdaQueryWrapper.like(WorkSetting::getFheader, request.getFheader()); 
		    } 
             
        if (StringUtils.hasText(request.getFcreateuser())) {
			    lambdaQueryWrapper.like(WorkSetting::getFcreateuser, request.getFcreateuser()); 
		    } 
             
        if (request.getFcreatedate() != null){
        	Date[] fcreatedate=request.getFcreatedate();
        	if(fcreatedate.length>0)
        	  lambdaQueryWrapper.ge(WorkSetting::getFcreatedate, fcreatedate[0]); 
        	if(fcreatedate.length==2)
        	  lambdaQueryWrapper.le(WorkSetting::getFcreatedate,fcreatedate[1]);      
        	
        }    
             
        
		lambdaQueryWrapper.orderByDesc(WorkSetting::getId);	
		Page<WorkSetting> selectPage = this.baseMapper.selectPage(page, lambdaQueryWrapper);
		return selectPage;
    }


	@Override
	public boolean saveworkorder(String userName, Map<String, Object> header, List<Map<String, Object>> datavsList,List<Map<String, Object>> datatestList) {
		//获取当前日期
		Date currentDate = new Date();
		List<WorkSetting> workSettingList = new ArrayList<>();
		WorkSetting workSetting = new WorkSetting();
		//生成工单单据编号
		String fbillno = IXqcConst.createBillNo(BillType.JHGD);
		//保存工单单据头
		if (header!=null)
		{
			workSetting.setFbillno(fbillno);
			workSetting.setFname(VtuzxUtil.getString(header,"fname"));
			workSetting.setFcustomer(VtuzxUtil.getString(header,"fcustomer"));
			workSetting.setFheader(VtuzxUtil.getString(header,"fheader"));//负责人
			workSetting.setFtype(VtuzxUtil.getInt(header,"ftype"));
			workSetting.setFstatus(0);
			workSetting.setFnote(VtuzxUtil.getString(header,"fnote"));
			workSetting.setFcreateuser(userName);
			workSetting.setFcreatedate(currentDate);
			workSetting.setFvs(VtuzxUtil.getInt(header,"fvs"));
			workSetting.setFvcom(VtuzxUtil.getInt(header,"fvcom"));

			workSettingList.add(workSetting);
		}
		boolean res = workSettingService.saveBatch(workSettingList);
		List<Workvs> workvsList = new ArrayList<>();
		//获取单据头内码关联信息保存VS波形数据
		long detailid = workSetting.getId();
		for (Map<String, Object> workvsmap : datavsList) {
			Workvs workvs = new Workvs();
			workvs.setFdetailid(detailid);
			workvs.setFnumber(VtuzxUtil.getString(workvsmap,"vsNumber"));
			workvs.setFvcom(VtuzxUtil.getDouble(workvsmap,"fvcom"));
			workvs.setFemperature(VtuzxUtil.getString(workvsmap,"femperature"));
			workvs.setFdianya(VtuzxUtil.getDouble(workvsmap,"fdianya"));
			workvs.setFboxing(VtuzxUtil.getInt(workvsmap,"boxing"));
			workvs.setFresidtime(VtuzxUtil.getDouble(workvsmap,"fresidtime"));
			workvs.setFrefreshtime(VtuzxUtil.getDouble(workvsmap,"Frefreshtime"));
			workvs.setFtuopanqty(VtuzxUtil.getLong(workvsmap,"ftuopanqty"));
			workvs.setFnote(VtuzxUtil.getString(workvsmap,"fnote"));
			workvs.setFbxid(VtuzxUtil.getInt(workvsmap,"fid"));
			workvs.setFpicture(VtuzxUtil.getInt(workvsmap,"fpictureId"));
			workvs.setFiszx(0);
			workvs.setIsqd(0);
			workvsList.add(workvs);
		}
		boolean resworkvs = workvsService.saveOrUpdateBatch(workvsList);
		//根据detailid保存测试波形数据
		List<Worktest> worktestList = new ArrayList<>();
		for (Map<String, Object> worktestmap : datatestList) {
			Worktest worktest = new Worktest();
			worktest.setFdetailid(detailid);
			worktest.setFnumber(VtuzxUtil.getString(worktestmap,"fnumber"));
			worktest.setFvs(VtuzxUtil.getDouble(worktestmap,"fvs"));
			worktest.setFvcom(VtuzxUtil.getDouble(worktestmap,"fvcom"));
			worktest.setFemperature(VtuzxUtil.getString(worktestmap,"femperature"));
			worktest.setFdianya(VtuzxUtil.getDouble(worktestmap,"fdianya"));
			worktest.setFboxing(VtuzxUtil.getInt(worktestmap,"boxing"));
			worktest.setFresidtime(VtuzxUtil.getDouble(worktestmap,"fcresidtime"));
			worktest.setFrefreshtime(VtuzxUtil.getDouble(worktestmap,"frefreshtime"));
			worktest.setFtuopanqty(VtuzxUtil.getLong(worktestmap,"ftuopanqty"));
			worktest.setFnote(VtuzxUtil.getString(worktestmap,"fnote"));
			worktest.setFbxid(VtuzxUtil.getInt(worktestmap,"fid"));
			worktest.setFpicture(VtuzxUtil.getInt(worktestmap,"fpicture"));
			worktest.setFiszx(0);
			worktest.setIsqd(0);
			worktestList.add(worktest);
		}
		boolean resworktest = worktestService.saveOrUpdateBatch(worktestList);
		return  res && resworkvs && resworktest;
	}

	/**
	 * 修改工单
	 * @param userName
	 * @param header
	 * @param datavsList
	 * @param datatestList
	 * @return
	 */
	@Override
	public boolean updateworkorder(String userName, Map<String, Object> header, List<Map<String, Object>> datavsList,List<Map<String ,Object>> datatestList) {
		//获取当前日期
		Date currentDate = new Date();
		List<WorkSetting> workSettingList = new ArrayList<>();
		WorkSetting workSetting = new WorkSetting();
		if (header!=null)
		{
			workSetting.setId(VtuzxUtil.getLong(header,"id"));
			workSetting.setFname(VtuzxUtil.getString(header,"fname"));
			workSetting.setFcustomer(VtuzxUtil.getString(header,"fcustomer"));
			workSetting.setFheader(VtuzxUtil.getString(header,"fheader"));//负责人
			workSetting.setFtype(VtuzxUtil.getInt(header,"ftype"));
			workSetting.setFstatus(0);
			workSetting.setFnote(VtuzxUtil.getString(header,"fnote"));
			workSetting.setFupdateuser(userName);
			workSetting.setFupdatedate(currentDate);
			workSetting.setFvs(VtuzxUtil.getInt(header,"fvs"));
			workSetting.setFvcom(VtuzxUtil.getInt(header,"fvcom"));
			workSettingList.add(workSetting);
		}
		boolean res = workSettingService.saveOrUpdateBatch(workSettingList);
		long detailid = workSetting.getId();

		//删除关联表数据，重新新增
		workvsService.remove(new QueryWrapper<Workvs>().eq("fdetailid",detailid));
		worktestService.remove(new QueryWrapper<Worktest>().eq("fdetailid",detailid));

		List<Workvs> workvsList = new ArrayList<>();
		for (Map<String, Object> workvsmap : datavsList) {
			Workvs workvs =new Workvs();
			//workvs.setId(VtuzxUtil.getLong(workvsmap,"id"));
			workvs.setFdetailid(detailid);
			workvs.setFnumber(VtuzxUtil.getString(workvsmap,"vsNumber"));
			workvs.setFvcom(VtuzxUtil.getDouble(workvsmap,"fvcom"));
			workvs.setFemperature(VtuzxUtil.getString(workvsmap,"femperature"));
			workvs.setFdianya(VtuzxUtil.getDouble(workvsmap,"fdianya"));
			workvs.setFboxing(VtuzxUtil.getInt(workvsmap,"boxing"));
			workvs.setFresidtime(VtuzxUtil.getDouble(workvsmap,"fresidtime"));
			workvs.setFrefreshtime(VtuzxUtil.getDouble(workvsmap,"frefresjtime"));
			workvs.setFtuopanqty(VtuzxUtil.getLong(workvsmap,"ftuopanqty"));
			workvs.setFpicture(VtuzxUtil.getInt(workvsmap,"fpicture"));
			workvs.setFnote(VtuzxUtil.getString(workvsmap,"fnote"));
			workvsList.add(workvs);
		}
		boolean resworkvs = workvsService.saveOrUpdateBatch(workvsList);
		List<Worktest> worktestList = new ArrayList<>();
		for (Map<String, Object> worktestmap : datatestList) {
			Worktest worktest = new Worktest();
			worktest.setId(VtuzxUtil.getLong(worktestmap,"id"));
			worktest.setFdetailid(detailid);
			worktest.setFnumber(VtuzxUtil.getString(worktestmap,"vsNumber"));
			worktest.setFvs(VtuzxUtil.getDouble(worktestmap,"fvs"));
			worktest.setFvcom(VtuzxUtil.getDouble(worktestmap,"fvcom"));
			worktest.setFemperature(VtuzxUtil.getString(worktestmap,"femperature"));
			worktest.setFdianya(VtuzxUtil.getDouble(worktestmap,"fdianya"));
			worktest.setFboxing(VtuzxUtil.getInt(worktestmap,"boxing"));
			worktest.setFresidtime(VtuzxUtil.getDouble(worktestmap,"fresidtime"));
			worktest.setFrefreshtime(VtuzxUtil.getDouble(worktestmap,"frefreshtime"));
			worktest.setFtuopanqty(VtuzxUtil.getLong(worktestmap,"ftuopanqty"));
			worktest.setFpicture(VtuzxUtil.getInt(worktestmap,"fpicture"));
			worktest.setFnote(VtuzxUtil.getString(worktestmap,"fnote"));
			worktestList.add(worktest);
		}
		boolean resworktest = worktestService.saveOrUpdateBatch(worktestList);
		return  res && resworkvs && resworktest;
	}

	@Override
	public VtuzxMap getDetail(Long id) {
		WorkSetting workSetting = workSettingDao.selectById(id);
		List<Worktest> testList = worktestDao.selectList(new QueryWrapper<Worktest>().eq("fdetailid", id));
		List<Workvs> vsList = workvsDao.selectList(new QueryWrapper<Workvs>().eq("fdetailid", id));
		VtuzxMap data = new VtuzxMap();
		data.append("header",workSetting)
				.append("testList",testList)
				.append("vsList",vsList);
		return data;
	}

	@Override
	public void sendWorkMessage(WorkSetting workSetting) {

		Integer fvcom = workSetting.getFvcom();
		Integer fvs = workSetting.getFvs();
		String fbillno = workSetting.getFbillno();//工单号
		int status = 1;
		Map<String,Object> data = new HashMap<>();
		String promrt = "";
		if(fvcom == null || fvcom == 0){
			status += 2;
			promrt += "VCom值未录入,";
		}
		if (fvs == null || fvs == 0){//vs值未录入
			status += 4;
			promrt += "VS值未录入,";
		}
		data.put("FBILLNO",fbillno);
		data.put("FSTATE",status);
		data.put("FVCOM",fvcom);
		data.put("FVS",fvs);
		data.put("FPROMPT",promrt);
		JSONObject jsonObject = new JSONObject(data);
		providerConfig.publish(1,true,"awf_test_info/" + fbillno,jsonObject.toString());

	}


}